package com.csii.autocode.service.impl;

import cn.hutool.core.util.StrUtil;
import com.csii.autocode.config.ReadConfig;
import com.csii.autocode.config.ReadTemplate;
import com.csii.autocode.entity.ColumnVo;
import com.csii.autocode.entity.TableVo;
import com.csii.autocode.service.CommonCodeGenerate;
import com.csii.autocode.service.IGenerate;
import com.csii.autocode.utils.DbReadTableUtil;
import com.csii.autocode.utils.NonceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CodeGenerateOne extends CommonCodeGenerate implements IGenerate {
    private static final Logger logger = LoggerFactory.getLogger(CodeGenerateOne.class);

    private TableVo tableVo;

    private List<ColumnVo> columnVos;

    private List<ColumnVo> columnVoList;

    public CodeGenerateOne(TableVo tableVo) {
        this.tableVo = tableVo;
    }

    public CodeGenerateOne(TableVo tableVo, List<ColumnVo> columns, List<ColumnVo> originalColumns) {
        this.tableVo = tableVo;
        this.columnVos = columns;
        this.columnVoList = originalColumns;
    }

    public Map<String, Object> getCreateInfo() throws Exception {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("bussiPackage", ReadConfig.getBassiPackage());
        hashMap.put("entityPackage", this.tableVo.getEntityPackage());
        hashMap.put("entityName", this.tableVo.getEntityName());
        hashMap.put("tableName", this.tableVo.getTableName());
        hashMap.put("primaryKeyField", ReadConfig.getDbTableId());
        if (this.tableVo.getFieldRequiredNum() == null)
            this.tableVo.setFieldRequiredNum(Integer.valueOf(StrUtil.isNotEmpty(ReadConfig.getField_required_num()) ? Integer.parseInt(ReadConfig.getField_required_num()) : -1));
        if (this.tableVo.getSearchFieldNum() == null)
            this.tableVo.setSearchFieldNum(Integer.valueOf(StrUtil.isNotEmpty(ReadConfig.getPageSearchFiledNum()) ? Integer.parseInt(ReadConfig.getPageSearchFiledNum()) : -1));
        if (this.tableVo.getFieldRowNum() == null)
            this.tableVo.setFieldRowNum(Integer.valueOf(Integer.parseInt(ReadConfig.getField_row_num())));
        hashMap.put("tableVo", this.tableVo);
        try {
            if (this.columnVos == null || this.columnVos.size() == 0)
                this.columnVos = DbReadTableUtil.getTableColumn(this.tableVo.getTableName());
            hashMap.put("columns", this.columnVos);
            if (this.columnVoList == null || this.columnVoList.size() == 0)
                this.columnVoList = DbReadTableUtil.getVolumnVoList(this.tableVo.getTableName());
            hashMap.put("originalColumns", this.columnVoList);
            for (ColumnVo columnVo : this.columnVoList) {
                if (columnVo.getFieldName().toLowerCase().equals(ReadConfig.getDbTableId().toLowerCase()))
                    hashMap.put("primaryKeyPolicy", columnVo.getFieldType());
            }
        } catch (Exception exception) {
            throw exception;
        }
        long l = NonceUtils.randomLong() + NonceUtils.currentMills();
        hashMap.put("serialVersionUID", String.valueOf(l));
        logger.info("load template data: " + hashMap.toString());
        return hashMap;
    }

    //生成单表通用代码模型
    public List<String> generateCodeFile(String stylePath) throws Exception {
        logger.debug("----csii---Code----Generation----[单表模型:" + this.tableVo.getTableName() + "]------- 生成中。。。");
        String str1 = ReadConfig.getProjectPath();
        Map<String, Object> map = getCreateInfo();
        String str2 = ReadConfig.template_path;
        if (stringCut(str2, "/").equals("auto-code/code-template")) {
            str2 = "/" + stringCut(str2, "/") + "/one";
            ReadConfig.setTemplatePath(str2);
        }
        ReadTemplate readTemplate = new ReadTemplate(str2);
        readTemplate.setStylePath(stylePath);
        this.createAllFiles(readTemplate, str1, map);
        logger.info(" ----- csii-boot ---- generate  code  success =======> 表名：" + this.tableVo.getTableName() + " ");
        return this.outStrs;
    }

    public List<String> generateCodeFile(String projectPath, String templatePath, String stylePath) throws Exception {
        if (projectPath != null && !"".equals(projectPath))
            ReadConfig.setProjectPath(projectPath);
        if (templatePath != null && !"".equals(templatePath))
            ReadConfig.setTemplatePath(templatePath);
        generateCodeFile(stylePath);
        return this.outStrs;
    }
}
